cmake_minimum_required(VERSION 2.6)

project(sql)
find_library(HAVE_LIBMYSQL NAMES mysqlclient)
#check_include_files("mysql/mysql.h" HAVE_MYSQL_MYSQL_H)

if(HAVE_LIBMYSQL)

find_program(MYSQL_CONFIG NAMES mysql_config DOC "mysql_config")

execute_process(
	COMMAND ${MYSQL_CONFIG} --include
	OUTPUT_VARIABLE HAVE_MYSQL_H
	OUTPUT_STRIP_TRAILING_WHITESPACE
	)

if(HAVE_MYSQL_H)

set(VERSION_MAJOR "1")
set(VERSION_MINOR "0")
set(VERSION_PATCH "0")
set(KONOHA_VERSION  ${VERSION_MAJOR}.${VERSION_MINOR})

set(CMAKE_C_FLAGS_RELEASE "-Wall -O2 -g ${HAVE_MYSQL_H}")
set(CMAKE_C_FLAGS_DEBUG   "-Wall -O0 -g3 ${HAVE_MYSQL_H}")
set(CMAKE_BUILD_TYPE Release)
#set(CMAKE_BUILD_TYPE Debug)

set(PACKAGE_SCRIPT_CODE sql.k)
set(PACKAGE_SOURCE_CODE
	src/dbapi.c
	src/query.c
	src/mysql.c
)

set(PACKAGE_NAME    ${PROJECT_NAME})
set(PACKAGE_VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH})
set(PACKAGE_STRING  ${CMAKE_PROJECT_NAME}-${PACKAGE_VERSION})
set(KONOHA_PACKAGE_DIR konoha/package/${KONOHA_VERSION}/konoha.${PROJECT_NAME})

set(INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}
		${CMAKE_CURRENT_SOURCE_DIR}/include  ${CMAKE_CURRENT_BINARY_DIR}
		${KONOHA_INCLUDE_DIRS}
		${MYSQL_H}
		)
include_directories(${INCLUDE_DIRS})

add_definitions(-D_SETUP)

add_library(${PACKAGE_NAME} SHARED ${PACKAGE_SOURCE_CODE})
set_target_properties(${PACKAGE_NAME} PROPERTIES PREFIX "")
target_link_libraries(${PACKAGE_NAME} konoha)
target_link_libraries(${PACKAGE_NAME} ${HAVE_LIBMYSQL})

install(TARGETS ${PACKAGE_NAME} DESTINATION ${KONOHA_PACKAGE_DIR})
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${PACKAGE_SCRIPT_CODE} DESTINATION ${KONOHA_PACKAGE_DIR})

endif(HAVE_MYSQL_H)
endif(HAVE_LIBMYSQL)
